package com.roncoo.education.course.listener;

import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.roncoo.education.course.service.dao.CourseDao;
import com.roncoo.education.course.service.dao.UserCourseAppointmentDao;
import com.roncoo.education.course.service.dao.impl.mapper.entity.Course;
import com.roncoo.education.course.service.dao.impl.mapper.entity.UserCourseAppointment;
import com.roncoo.education.util.dingd.DingDUtil;
import com.roncoo.education.util.enums.AppointmentCourse;
import com.roncoo.education.util.enums.IsAppointment;
import com.roncoo.education.util.enums.IsOpenEnum;
import com.roncoo.education.util.enums.IsSendEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private RedisMessageListenerContainer redisMessageListenerContainer;

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private UserCourseAppointmentDao userCourseAppointmentDao;

    @Autowired
    private DingDUtil dingDUtil;

    /**
     * 构造方法实例化时传入监听容器
     * @param redisMessageListenerContainer
     */
    public RedisKeyExpirationListener(RedisMessageListenerContainer redisMessageListenerContainer) {
        super(redisMessageListenerContainer);
    }

    /**
     * 无法获取内容，只能获取到key
     *
     * @param message key
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern)  {
        //message是两个双引号包起来的字符串
        String substring = message.toString().substring(1, message.toString().length() - 1);
        boolean with = substring.startsWith(AppointmentCourse.DEADlINE_PREFIX);
        if(!with){
            return;
        }
        String courseId = substring.replace(AppointmentCourse.DEADlINE_PREFIX, "");

        //获取课程信息
        Course course = courseDao.getById(Long.valueOf(courseId));
        if(course==null){
            return;
        }
        if(course.getStatusId()==0){
            return;
        }
        if(course.getIsPutaway()==0){
            return;
        }
        //获取该课程的预约用户
        List<UserCourseAppointment> userCourseAppointmentList = userCourseAppointmentDao.getByCourseId(course.getId());
        // TODO 目前ip不在白名单
        String accessToken = dingDUtil.getAccessToken();
        for (UserCourseAppointment userCourseAppointment : userCourseAppointmentList) {
            OapiMessageCorpconversationAsyncsendV2Response oapiMessageCorpconversationAsyncsendV2Response =
                    dingDUtil.sendMessage(userCourseAppointment.getDingUserId(), course.getCourseName() + "即将开课了！",accessToken);
            //更新用户预约状态 为 已发送 通知
            userCourseAppointment.setIsSend(IsSendEnum.YES.getCode());
            userCourseAppointment.setGmtModified(LocalDateTime.now());
            userCourseAppointmentDao.updateById(userCourseAppointment);
        }

        //修改课程状态
        Course course1 = new Course();
        course1.setIsAppointment(IsAppointment.NO.getCode());
        course1.setId(course.getId());
        course1.setIsOpen(IsOpenEnum.YES.getCode());
        courseDao.updateById(course1);
    }
}
